package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"perm-master/cache"
	"perm-master/model"
	"strconv"
	"time"
)

func GoodAdd(c *gin.Context) {
	goodCode := c.PostForm("good_code")
	goodName := c.PostForm("good_name")
	if goodCode == "" || goodName == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	good := model.Good{
		GoodCode: goodCode,
		GoodName: goodName,
	}
	err := model.DB.Create(&good).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func GoodOrderAdd(c *gin.Context) {
	orderCode := c.PostForm("order_code")
	orderPrice, _ := strconv.Atoi(c.PostForm("order_price"))
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	if orderCode == "" || orderPrice == 0 || gid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不为空",
		})
		return
	}
	goodOrder := model.GoodOrder{
		OrderCode:  orderCode,
		OrderPrice: orderPrice,
		Gid:        gid,
	}
	err := model.DB.Create(&goodOrder).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func GoodOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
			SELECT
		  good_orders.id as GoodOrderId,
		  goods.good_code as GoodCode,
		  goods.good_name as GoodName,
		  good_orders.order_code as OrderCode,
		  good_orders.order_price as OrderPrice
		FROM
		  good_orders
		LEFT JOIN goods ON goods.id = good_orders.gid;
	*/
	var list []model.GoodOrderList
	redis_key := "good_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("good_orders").
			Select("good_orders.id as GoodOrderId",
				"goods.good_code as GoodCode",
				"goods.good_name as GoodName",
				"good_orders.order_code as OrderCode",
				"good_orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	model.DB.Model(&model.GoodOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  500,
		"msg":   "查询成功",
		"data":  list,
		"total": total,
	})
	return
}

func GoodOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list model.GoodOrderList
	redis_key := "good_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.Table("good_orders").
			Select("good_orders.id as GoodOrderId",
				"goods.good_code as GoodCode",
				"goods.good_name as GoodName",
				"good_orders.order_code as OrderCode",
				"good_orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN goods ON goods.id = good_orders.gid").
			Where("good_orders.id = ?", id).
			Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
